<template>
  <div>
    <el-card shadow="never" style="margin-bottom: 10px">
      <el-form :inline="true" :model="formInline">
        <el-form-item label="角色名称">
          <el-input v-model="formInline.roleName" placeholder="角色名称" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getFindSysRoleList" :icon="Search" :loading="loading">搜索</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <TableComponent :tableData="tableData" :column="tableColumn" @getAddorEdit="getAddorEdit" @getRefresh="getFindSysRoleList" tableTitle="角色列表" :isShowPaging="false" v-loading="loading">
      <template v-slot:text="{ scope, item }">
        <span>{{ scope.row[item.field] }}</span>
      </template>
      <template v-slot:roleStatus="{ scope, item }">
        <span>{{ scope.row[item.field] == 1 ? '启用' : '停用' }}</span>
      </template>
      <template v-slot:operation="{ scope, item }">
        <el-button type="primary" size="small" @click="getAddorEdit(scope.row)">编辑</el-button>
        <el-popconfirm title="Are you sure to delete this?" @confirm="getRemoveSysRoleById({ roleCode: scope.row.roleCode })">
          <template #reference>
            <el-button type="danger" size="small">删除</el-button>
          </template>
        </el-popconfirm>
      </template>
    </TableComponent>
    <el-dialog v-model="dialogTableVisible" title="新增" width="600">
      <el-form :model="form.sysRoleDo" label-width="auto" style="max-width: 600px">
        <el-form-item label="角色名称">
          <el-input v-model="form.sysRoleDo.roleName" />
        </el-form-item>
        <el-form-item label="角色编码">
          <el-input v-model="form.sysRoleDo.roleCode" />
        </el-form-item>
        <el-form-item label="角色状态">
          <el-radio-group v-model="form.sysRoleDo.roleStatus">
            <el-radio value="1">启用</el-radio>
            <el-radio value="0">停用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="角色权限">
          <el-tree ref="treeRef" style="max-width: 600px; min-width: 300px" :data="data" show-checkbox node-key="menuId" :props="defaultProps" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogTableVisible = false">取消</el-button>
          <el-button type="primary" @click="getAddSysRole">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup name="rolse">
  import { Search } from '@element-plus/icons-vue'
  import { ElMessage } from 'element-plus'
  import { reactive, ref, nextTick } from 'vue'
  import type { SysRoleMenuDto, SysRoleDo } from './type/index'
  import { findSysRoleList, removeSysRoleAndRoleMenuAndUserRoleByRoleCode, saveOrUpdateSysRoleAndSysRoleMenu } from '@/api/sysRole'
  import { getMenuIdByRoleCode, sysMenuTree } from '@/api/sysMenu'
  import { ElTree } from 'element-plus'
  import TableComponent from '@/components/TableComponent/TableComponent.vue'
  const dialogTableVisible = ref(false)
  const formInline = reactive({
    roleName: '',
  })
  const tableData = ref([])
  let form = reactive<SysRoleMenuDto>({
    sysRoleDo: {
      roleCode: '',
      roleName: '',
      roleStatus: '1',
    },
    menuIds: [],
  })
  const loading = ref<boolean>(false)
  const tableColumn = [
    { label: '角色名称', field: 'roleName', slotName: 'text' },
    { label: '角色编码', field: 'roleCode', slotName: 'text' },
    { label: '角色状态', field: 'roleStatus', slotName: 'roleStatus' },
    { label: '操作', field: 'operation', slotName: 'operation', width: 200 },
  ]
  const defaultProps = {
    children: 'children',
    label: 'title',
  }
  const data = ref([])
  const treeRef = ref<InstanceType<typeof ElTree>>()
  const getAddSysRole = () => {
    form.menuIds = treeRef.value?.getCheckedKeys() as number[]
    saveOrUpdateSysRoleAndSysRoleMenu(form).then((res) => {
      getFindSysRoleList()
      dialogTableVisible.value = false
      ElMessage({
        type: res.data ? 'success' : 'error',
        message: res.data ? '创建成功' : '创建失败',
      })
    })
  }
  const getFindSysRoleList = () => {
    loading.value = true
    findSysRoleList({}).then((res) => {
      tableData.value = res.data
      loading.value = false
    })
  }
  getFindSysRoleList()
  async function getAddorEdit(row: SysRoleDo) {
    if (row.roleCode) {
      await getMenuIdByRoleCode({ roleCode: row.roleCode }).then((res) => {
        form.sysRoleDo = { ...row }
        form.menuIds = [...res.data]
      })
    } else {
      form.sysRoleDo = {
        roleCode: '',
        roleName: '',
        roleStatus: '1',
      }
      form.menuIds = []
    }
    console.log(form)

    dialogTableVisible.value = true
    nextTick(() => {
      treeRef.value!.setCheckedKeys(form.menuIds as any)
    })
  }

  function getSysMenuTree() {
    sysMenuTree({}).then((res) => {
      data.value = res.data
    })
  }
  function getRemoveSysRoleById(params: any) {
    removeSysRoleAndRoleMenuAndUserRoleByRoleCode(params).then((res) => {
      getFindSysRoleList()
    })
  }
  getSysMenuTree()
</script>

<style lang="scss"></style>
